package com.mytest.study.shortUrl;


public class ShortUrlGenerator {

    public static void main(String[] args) {
        String urlRoot = "https://www.elastic.co/";
        String sLongUrl = "guide/cn/elasticsearch/guide/2.x/bulk.html";
        for (String shortUrl : shortUrl(sLongUrl)) {
            System.out.println(urlRoot + shortUrl);
        }
    }

    public static String[] shortUrl(String url) {
        String[] chars = new String[]{
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
                "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
                "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
        };
        String hex = (Encript.md5(url));
        String[] resUrl = new String[4];
        for (int i = 0; i < 4; i++) {
            String sTempSubString = hex.substring(i * 8, i * 8 + 8);
            long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
            StringBuilder outChars = new StringBuilder();
            for (int j = 0; j < 6; j++) {
                long index = 0x0000003D & lHexLong;
                outChars.append(chars[(int) index]);
                lHexLong = lHexLong >> 5;
            }
            resUrl[i] = outChars.toString();
        }
        return resUrl;
    }
}
